package com.softdev.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.softdev.common.R;
import com.softdev.service.ModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api("模块操作")
@Slf4j
@RestController
@RequestMapping("/module")
public class ModuleController {

    @Resource
    private ModuleService moduleService;

    @ApiOperation("更新模块进度")
    @PutMapping("/up")
    public R<String> update(Long projectId , Long moduleId) throws JsonProcessingException {

        if (!moduleService.hasAuthor(moduleId)) {
            return R.error("你不是该模块成员！");
        }

        if (moduleService.update(projectId, moduleId)) {
            return R.success("模块进度更新成功！");
        }

        return R.error("模块进度更新失败...");
    }

    @ApiOperation("回滚模块进度")
    @PreAuthorize("hasRole('ROLE_manager')")
    @PutMapping("/rbk")
    public R<String> rollback(Long projectId , Long moduleId) throws JsonProcessingException {

        if (moduleService.rollback(projectId, moduleId)) {
            return R.success("模块进度回滚成功！");
        }

        return R.error("模块进度回滚失败...");
    }


}
